// pages/index/index.js
var com = require("../../utils/util.js");
var page = 1;
var total_page = 0;
var cat_id = 0;
var list_order = 0;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    order: 0,
    min_height: 0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var sync = wx.getSystemInfoSync();
    this.setData({
      min_height: sync.windowHeight
    })
    cat_id = e.fid;
    com.post('Api/Home/shop_list', { fid: cat_id, order: list_order, page: page}, "setContent", this);
  },
  setContent: function (e) {
    total_page = e.total;
    this.setData({
      list: this.data.list.concat(e.lists),
    });
  },
  /**
   * 修改排序
   */
  save_order: function (e) {
    list_order = e.currentTarget.dataset.order;
    page = 1;
    this.setData({
      order: list_order
    });
    com.post('Api/Home/shop_list', { fid: cat_id, order: list_order, page: page }, "setContents", this);
  },
  setContents: function (e) {
    total_page = e.total;
    this.setData({
      list: e.lists,
    });
  },
  /**
   * 下拉显示更多
   */
  onList: function () {
    page = page + 1;
    if (total_page >= page) {
      com.post('Api/Home/shop_list', { fid: cat_id, order: list_order, page: page }, "setContent", this);
    }
  },
  /**
   * 进入详情
   */
  goDetails: function (e) {
    wx.navigateTo({
      url: "details?id=" + e.currentTarget.dataset.id,
    })
  },
  /**
   * 关闭页面，页面归1。防止下次进入页面出错
   */
  onUnload : function(){
    page = 1;
  }
})